package com.ruoyi.hxf.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.hxf.domain.Pay;
import com.ruoyi.hxf.domain.Pd;
import com.ruoyi.hxf.domain.Subject;
import com.ruoyi.hxf.domain.bo.PayBo;
import com.ruoyi.hxf.domain.bo.SubjectBo;
import com.ruoyi.hxf.domain.vo.PayVo;
import com.ruoyi.hxf.mapper.PayMapper;
import com.ruoyi.hxf.mapper.PdMapper;
import com.ruoyi.hxf.mapper.TypeMapper;
import com.ruoyi.hxf.service.IPayService;
import com.ruoyi.hxf.service.IPdService;
import jdk.nashorn.internal.runtime.Undefined;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 缴费(推送服务模块)Service业务层处理
 *
 * @author hxf
 * @date 2023-01-12
 */
@RequiredArgsConstructor
@Service
public class PdServiceImpl implements IPdService {

    private final PdMapper pdMapper;
    private QueryWrapper<Pd> wrapper;

    /**
     * 查询缴费列表
     */
    @Override
    public TableDataInfo<Pd> queryPageList(Pd pd, PageQuery pageQuery, String userName, String nikeName) {
        Page<Pd> result = pdMapper.selectPageList(pageQuery.build(), this.buildQueryWrapper(pd, userName, nikeName));
        return TableDataInfo.build(result);
    }

    @Override
    public List<Pd> selectPdByPayId(Long payId) {
        return pdMapper.selectPdByPayId(payId);
    }

    /**
     * 查询缴费(推送服务模块)
     */
    @Override
    public Pd queryById(Long userId, Long payId) {
        return pdMapper.selectVoByPayId(userId, payId);
    }

    /**
     * 缴费（修改状态）
     *
     * @param pd
     * @return
     */
    @Override
    public int MyUpdateByBo(Pd pd) {
        return pdMapper.MyUpdateByBo(pd);
    }

    @Override
    public TableDataInfo<Pd> queryPagePdList(Pd pd, PageQuery pageQuery) {
        Page<Pd> result = pdMapper.selectPageList(pageQuery.build(), this.buildQueryPdWrapper(pd));
        return TableDataInfo.build(result);
    }

    private Wrapper<Pd> buildQueryPdWrapper(Pd pd) {
        Map<String, Object> params = pd.getParams();
        wrapper = Wrappers.query();
        if (params.get("endCreateTime") != null) {
            String dt = String.valueOf(params.get("endCreateTime"));  // Start date
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            try {
                c.setTime(sdf.parse(dt));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            c.add(Calendar.DATE, 1);  // number of days to add
            dt = sdf.format(c.getTime());  // dt is now the new date
            params.put("endCreateTime", dt);
        }
        wrapper.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            "fd.create_time", params.get("beginCreateTime"), params.get("endCreateTime"));
        wrapper.eq(StringUtils.isNotBlank(pd.getCreateBy()), "fd.create_by", pd.getCreateBy());
        wrapper.eq("fd.my_is_delete", 0);
        wrapper.eq("fd.status", 1);
        wrapper.last("order by fd.create_time desc");
        return wrapper;
    }

    private Wrapper<Pd> buildQueryWrapper(Pd pd, String userName, String nikeName) {
        wrapper = Wrappers.query();
        wrapper.eq(pd.getPayId() != null, "fd.pay_id", pd.getPayId());
        wrapper.eq(StringUtils.isNotBlank(pd.getStatus()), "fd.status", pd.getStatus());
        wrapper.like(!nikeName.equals("undefined"), "su.nick_name", nikeName);
        wrapper.like(!userName.equals("undefined"), "su.user_name", userName);
        wrapper.last("order by fd.create_time desc");
        return wrapper;
    }

    /**
     * 批量删除缴费(推送服务模块)
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        for (Long id : ids) {
            Pd pd = pdMapper.selectVoById(id);
            pd.setMyIsDelete("1");
            pdMapper.updateById(pd);
        }
        return true;
    }

    @Override
    public Boolean deleteRecordWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        for (Long id : ids) {
            Pd pd = pdMapper.selectVoById(id);
            pd.setIsDelete("1");
            pdMapper.updateById(pd);
        }
        return true;
    }

    @Override
    public TableDataInfo<Pd> queryPageRecordList(Pd pd, PageQuery pageQuery, String userName, String nikeName, String payName) {
        Page<Pd> result = pdMapper.selectPageList(pageQuery.build(), this.buildQueryRecordWrapper(pd, userName, nikeName, payName));
        return TableDataInfo.build(result);
    }



    /**
     * 查询缴费(推送服务模块)列表
     */

    private Wrapper<Pd> buildQueryRecordWrapper(Pd pd, String userName, String nikeName, String payName) {
        wrapper = Wrappers.query();
        wrapper.eq(pd.getPayId() != null, "fd.pay_id", pd.getPayId());
        wrapper.eq(StringUtils.isNotBlank(pd.getStatus()), "fd.status", pd.getStatus());
        wrapper.like(!nikeName.equals("undefined"), "su.nick_name", nikeName);
        wrapper.like(!userName.equals("undefined"), "su.user_name", userName);
        wrapper.like(!payName.equals("undefined"), "fp.pay_name", payName);
        wrapper.eq("fd.is_delete", 0);
        wrapper.eq(StringUtils.isNotBlank(pd.getRemark()), "fa.class_id", pd.getRemark());
        wrapper.last("order by fd.create_time desc");
        return wrapper;
    }
}
